import {Component, OnInit} from '@angular/core';
import {WebsiteService} from '../../../service/website.service';

@Component({
  selector: 'app-index',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {
  bannerCovers = []; // 轮播图类别封面
  serviceCovers = []; // 热门服务类别封面
  recommendGoods = []; // 店主推荐商品

  /**
   * 构造函数
   */
  constructor(private websiteService: WebsiteService) {
  }

  /**
   * 初始化函数
   */
  ngOnInit() {
    // 查询封面类别信息，包含封面信息
    this.websiteService.selectCoverTypeJoinCover({names: ['二维码', '轮播图', '热门服务']}).subscribe(coverTypeRes => {
      if (coverTypeRes.code === 0) {
        coverTypeRes.data.map(coverType => {
          if (coverType.name.includes('轮播图')) {
            this.bannerCovers = coverType.children;
          } else if (coverType.name.includes('热门服务')) {
            this.serviceCovers = coverType.children;
          } else if (coverType.name.includes('二维码')) {
            this.websiteService.pushWebsiteVariable({weixinCodeCovers: coverType.children}); // 推送消息对象到父组件
          }
        });
      }
    }, () => {}, () => {
      // 查询商品信息
      this.websiteService.selectGoods({isActive: '0', isRecommend: '0'}).subscribe(goodsRes => {
        if (goodsRes.code === 0) {
          this.recommendGoods = goodsRes.data;
        }
      });
    });
  }

}
